package com.kt.emall.product.web;

import com.kt.emall.product.entity.CategoryEntity;
import com.kt.emall.product.service.CategoryService;
import com.kt.emall.product.vo.Catelog2VO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Description: 首页跳转
 * Created with IntelliJ IDEA.
 *
 * @author: KamTeng
 * Date: 2021/8/10
 */
@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        // TODO 1、查出所有的 1 级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevelOneCategories();
        model.addAttribute("categories", categoryEntities);
        return "index";
    }

    @ResponseBody // 将返回的数据以json格式返回
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catelog2VO>> getCatelogJson() {
        Map<String, List<Catelog2VO>> catelogJson = categoryService.getCatelogJson();
        return catelogJson;
    }
}